﻿


using System;
/**
* 将.class文件读到内存中
* created by Arosy
*/
public class ClassRead:FileC
{
	private byte[] data;
	private int cur = 0;
	//构造方法
	public ClassRead(ref byte[] data)
	{
		this.data = data;
	} //构造方法

	//根据路径读取.class文件

	/**
	 * 所用到的 htonl() 函数 为大端与小端转换 ，无需深究
	 */

	/**
	 * 根据路径读取.class文件
	 */
	public static ClassRead readByPath(string path)
	{
		FILE fp = fopen(path, "r"); //c++ 流的操作
                                    //fseek(fp, 0, SEEK_END);
                                    //string c = new string(new char[ftell(fp) - 1]); // ftell(fp)函数可获取此文件字节个数 ， 然后申请count个字节的内存空间
                                    //int cur = 0;
                                    //fseek(fp, 0, 0);
                                    ////下面将流中的字节复制到自己申请的内存中
                                    //while (true)
                                    //{
                                    //	byte s = fgetc(fp);
                                    //	*(c.Substring(cur)) = s;
                                    //	cur++;
                                    //	if (feof(fp))
                                    //	{
                                    //		break;
                                    //	}
                                    //}
        byte[] c = new byte[ftell(fp)];
        fread(c,fp, 0, c.Length);
		return new ClassRead(ref c); //返回一个ClassRead对象
	}


	/**
	 * 往后读取一个字节
	 */
	public byte readByOneByte()
	{
//C++ TO C# CONVERTER CRACKED BY X-CRACKER 2017 TODO TASK: C# does not have an equivalent to pointers to value types:
//ORIGINAL LINE: byte *res = (byte *)(data+cur);
		//byte res = (byte)(data.Substring(cur));
		byte res = (byte)(data[cur]);
        cur++;
		return res;
	}
	/**
	 * 往后读取2个字节
	 */
	public ushort readByTwoByte()
	{
//C++ TO C# CONVERTER CRACKED BY X-CRACKER 2017 TODO TASK: C# does not have an equivalent to pointers to value types:
//ORIGINAL LINE: ushort *res = (ushort *)(data+cur);
		//ushort res = (ushort)(data.Substring(cur));
		ushort res = (ushort)(BitConverter.ToUInt16( data,cur));
        cur = cur + 2;
		return htons(res);
	}
	/**
	 * 往后读取4个字节
	 */
	public uint readByFourByte()
	{
//C++ TO C# CONVERTER CRACKED BY X-CRACKER 2017 TODO TASK: C# does not have an equivalent to pointers to value types:
//ORIGINAL LINE: int *res = (int *)(data+cur);
		//int res = (int)(data.Substring(cur));
		uint res = (uint)(BitConverter.ToUInt32(data, cur));
        cur = cur + 4;
		return htonl(res);
	}
	/**
	 * 往后读取len个字节，并把值赋给target
	 */
	public void readByFreeByte(short len, byte[] target)
	{
		for (int i = 0; i < len; i++)
		{
			target[i] = readByOneByte();
		}
		target[len] = (byte)'\0';
	}


}
